package com.example.androidlearning2200;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import com.example.androidlearning2200.receiver.SplashReceiver;

/**
 * 用于测试广播接收器
 */
public class ReceiverActivity extends AppCompatActivity {

    private SplashReceiver loginReceiver1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiver);
        findViewById(R.id.btnSendBroadcast).setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setPackage(getPackageName());
            intent.setAction(MConstant.ACTION_OPEN_LOGIN);
            intent.putExtra("username", "admin");
            intent.putExtra("password", "admin123");
            sendBroadcast(intent);
        });
        findViewById(R.id.btnRegisterB).setOnClickListener(view -> {
            loginReceiver1 = new SplashReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(MConstant.ACTION_OPEN_SPLASH);
            registerReceiver(loginReceiver1, intentFilter);
            // 动态注册广播
        });
        findViewById(R.id.btnUnregister).setOnClickListener(view -> {
            // 取消注册广播
            if (loginReceiver1 != null) {
                unregisterReceiver(loginReceiver1);
            }
        });
        findViewById(R.id.btnSendBroadcast1).setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setPackage(getPackageName());
            intent.setAction(MConstant.ACTION_OPEN_SPLASH);
            intent.putExtra("username", "admin");
            intent.putExtra("password", "admin123");
            sendBroadcast(intent);
        });
    }
}